// FrogRenameAppendDialog.cpp : Implementation of CFrogRenameAppendDialog

#include "stdafx.h"
#include "FrogRenameAppendDialog.h"
#include "..\FilerFrogUtils\FilerFrogUtils.h"

// CFrogRenameAppendDialog
CFrogRenameAppendDialog::CFrogRenameAppendDialog(HICON hIcon):
CFrogProgressReportDialog(hIcon),
m_Postfix(L""),
m_Prefix(L"")
{
}


LRESULT CFrogRenameAppendDialog::OnInitDialog(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
	CFrogBaseDialog::OnInitDialog(uMsg, wParam, lParam, bHandled);
	DoDataExchange(FALSE);
	return 0;
}

LRESULT CFrogRenameAppendDialog::OnClickedOK(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
	bHandled = TRUE;
	// TODO: Add your control notification handler code here
	DoDataExchange(TRUE);

	if (m_Prefix!= L"" && !FilerFrogUtils::IsFileNameValid(wstring(m_Prefix)))
	{
		FrogMessageBox(m_hWnd, L"A file name cannot contain any of the following characters:\n\\ / : * ? \" < > |", L"File Name Validation", 0);
		return TRUE;
	}

	if (m_Postfix != L"" && !FilerFrogUtils::IsFileNameValid(wstring(m_Postfix)))
	{
		FrogMessageBox(m_hWnd, L"A file name cannot contain any of the following characters:\n\\ / : * ? \" < > |", L"File Name Validation", 0);
		return TRUE;
	}

	RunAsyncCommandWithProgressBar(false);
	EndDialog(wID);
	return 0;
}

void CFrogRenameAppendDialog::ExecuteCommand()
{
	m_FilesRefactor.RenameFilesAppend(m_LastResult, m_FilesList, (wstring)m_Prefix, (wstring)m_Postfix);
}

LRESULT CFrogRenameAppendDialog::OnEnChangeEditPostfix(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
	DoDataExchange(TRUE);
	SetPostPrePreview();
	return 0;
}

LRESULT CFrogRenameAppendDialog::OnEnChangeEditPrefix(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
	DoDataExchange(TRUE);
	SetPostPrePreview();
	return 0;
}

void CFrogRenameAppendDialog::SetPostPrePreview(wstring originalName)
{	
	//wstring originalName = FilerFrogUtils::GetFileNameFromPath(s_FilesList.front());

	wstring fileName = FilerFrogUtils::GetFileNameWithoutExtension(originalName);
	wstring fileExt = FilerFrogUtils::GetFileNameExtension(originalName);
	wstring newName = wstring(m_Prefix) + fileName + wstring(m_Postfix) + L"." + fileExt;

	wchar_t buffer[1024];
	swprintf_s(buffer, 1024, L"Before: %s\r\nAfter: %s", originalName.c_str(), newName.c_str());

	SetDlgItemText(IDC_STATIC_RENAME_PREVIEW, buffer);
}
